<template>
    <div id="login_form" style="margin: 0 auto; max-width: 400px; text-align: center;">
        <h4>登录串口管理系统</h4>
        <el-form ref="form" :model="form" label-width="100px" style="max-width:800px">
            <el-form-item label="账号">
                <el-input v-model="form.uid" placeholder="输入您的账号"></el-input>
            </el-form-item>
            <el-form-item label="密码">
                <el-input v-model="form.pwd" placeholder="输入您的密码"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="onSubmit">登录</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>
<script>
import VueCommon from '../VueCommon.js'
export default {
    created() {
        // 发起网络请求，检查是否初始化过了。
        // 如果已经初始化过了，则重定向到登录界面
        // 没初始化过，就停留在本界面
    },
    data() {
        let uid = VueCommon.store_getStr('uid')
        let pwd = VueCommon.store_getStr('pwd')
        return {
            form: {
                uid: uid,
                pwd: pwd
            }
        }
    },
    methods: {
        onSubmit() {
            let vm = this
            if (vm.isEmpty(vm.form.uid)) {
                vm.msgboxEx('账号不能为空', 'error')
                return
            }
            if (vm.isEmpty(vm.form.pwd)) {
                vm.msgboxEx('密码不能为空', 'error')
                return
            }
            vm.ajaxPost('/api/login', {
                uid: vm.form.uid,
                pwd: vm.form.pwd
            }, function(response) {
                if (response.status == 200) {
                    if (response.data.code == 1) {
                        vm.msgboxEx('登录成功', 'success')
                        VueCommon.saveLogin(vm.form.uid, vm.form.pwd)
                        window.location.href = "/main.html#/"
                        return
                    } else {
                        vm.msgboxEx('登录失败', 'error')
                    }
                }
            }, function(error) {
                console.log(error);
            })
        }
    },
    computed: {

    },
    mounted: function() {
        let vm = this
        if ((!vm.isEmpty(vm.form.uid)) && !(vm.isEmpty(vm.form.pwd))) {
            vm.ajaxPost('/api/login', {
                uid: vm.form.uid,
                pwd: vm.form.pwd
            }, function(response) {
                if (response.status == 200) {
                    if (response.data.code == 1) {
                        vm.msgboxEx('登录成功', 'success')
                        VueCommon.saveLogin(vm.form.uid, vm.form.pwd)
                        window.location.href = "/main.html#/"
                        return
                    }
                }
            }, function(error) {
                console.log(error);
            })
        }
    }
}
</script>
